package gui;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class circlecalc {
    public  circlecalc() {
        
            JFrame f = new JFrame();
			f.setVisible(true);
			f.setSize(400,400);
			// f.setExtendedState(JFrame.MAXIMIZED_BOTH);
			f.setLocationRelativeTo(null);
			f.setTitle("图形计算器");
			
			Container con = f.getContentPane();
			con.setLayout(new GridLayout(6,1));


            JPanel pan = new JPanel();
            JLabel circle = new JLabel("图形计算器");
            pan.add(circle);
            con.add(pan);
            Double pi = Math.PI;

            JPanel pan1 = new JPanel();
            JLabel type = new JLabel("图形的形状");
            Choice pattern = new Choice();
            pattern.add("圆形");
            pattern.add("三角形");
            pan1.add(type);
            pan1.add(pattern);
            con.add(pan1);



            JPanel pan2 = new JPanel();
            JLabel cs = new JLabel("图形的参数(空格隔开)");
            TextField parameter = new TextField(10);
            pan2.add(cs);
            pan2.add(parameter);
            con.add(pan2);


            JPanel pan3 = new JPanel();
            JLabel calc = new JLabel("计算");
            ButtonGroup grouptype = new ButtonGroup();
            JRadioButton circ = new JRadioButton("周长", true);
            JRadioButton sss = new JRadioButton("面积");
            grouptype.add(circ);grouptype.add(sss);
            pan3.add(calc);
            pan3.add(circ);pan3.add(sss);
            con.add(pan3);


            JPanel pan4 = new JPanel();
            JLabel result = new JLabel("计算结果");
            TextField consequence = new TextField(15);
            pan4.add(result);
            pan4.add(consequence);
            con.add(pan4);


            JPanel pan5 = new JPanel();
            Button calcpattern = new Button("计算");
            calcpattern.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String inputText = parameter.getText();
                    String[] values = inputText.split(" ");
                    Double result = null;


                   if(!parameter.getText().isEmpty()&&circ.isSelected()){
                    //计算周长
                    if(pattern.getSelectedItem().equals("圆形")){
                        result = Double.parseDouble(values[0])  *2 *pi;
                        String jieguo = String.valueOf(result);
                        consequence.setText(jieguo);
                        parameter.setText("");

                    }else if(pattern.getSelectedItem().equals("三角形")){
                        // result = Double.parseDouble(values[0])*Double.parseDouble(values[1])*Double.parseDouble(values[2]);
                        double side1 = Double.parseDouble(values[0]);
                        double side2 = Double.parseDouble(values[1]);
                        double side3 = Double.parseDouble(values[2]);
                        result = side1 + side2 +side3;
                        String jieguo = String.valueOf(result);
                        consequence.setText(jieguo);
                        parameter.setText("");
                    }else{
                        
                    }

                   }else if(!parameter.getText().isEmpty()&&sss.isSelected()){
                    //计算面积
                   if(pattern.getSelectedItem().equals("圆形")){
                    result = pi* Double.parseDouble(values[0])*Double.parseDouble(values[0]);
                    String jieguo = String.valueOf(result);
                    consequence.setText(jieguo);
                    parameter.setText("");
                   }else if(pattern.getSelectedItem().equals("三角形")){
                    double side1 = Double.parseDouble(values[0]);
                    double side2 = Double.parseDouble(values[1]);
                    double side3 = Double.parseDouble(values[2]);
                    double semiperimeter = (side1 + side2 + side3) / 2;
                    double area = Math.sqrt(semiperimeter * (semiperimeter - side1) * (semiperimeter - side2) * (semiperimeter - side3));
                    consequence.setText(String.valueOf(area));
                   }else {
                    
                   }
                   }else{
                    JOptionPane.showMessageDialog(f, "请完整输入参数", "错误", JOptionPane.ERROR_MESSAGE);
                        return;
                   }
                }
            });
            Button exit = new Button("退出");
            exit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    f.dispose();
                }
            });
            pan5.add(calcpattern);
            pan5.add(exit);
            con.add(pan5);
       
        
    }
}
